package com.ruoyi.project.stop.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.stop.domain.Remove;
import com.ruoyi.project.stop.domain.SensorRecordVO;
import com.ruoyi.project.stop.domain.ShipRecord;
import com.ruoyi.project.stop.dto.SensorRecordDTO;
import com.ruoyi.project.stop.mapper.ShipRecordMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.stop.mapper.SensorRecordMapper;
import com.ruoyi.project.stop.domain.SensorRecord;
import com.ruoyi.project.stop.service.ISensorRecordService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 传感器数据记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-26
 */
@Service
public class SensorRecordServiceImpl implements ISensorRecordService
{
    @Autowired
    private SensorRecordMapper sensorRecordMapper;

    @Autowired
    private ShipRecordMapper shipRecordMapper;

    /**
     * 查询传感器数据记录
     *
     * @param id 传感器数据记录主键
     * @return 传感器数据记录
     */
    @Override
    public SensorRecord selectSensorRecordById(Long id)
    {
        return sensorRecordMapper.selectSensorRecordById(id);
    }

    /**
     * 查询传感器数据记录列表
     *
     * @param sensorRecord 传感器数据记录
     * @return 传感器数据记录
     */
    @Override
    public List<SensorRecord> selectSensorRecordList(SensorRecord sensorRecord)
    {
        return sensorRecordMapper.selectSensorRecordList(sensorRecord);
    }

    @Override
    public List<SensorRecordDTO> selectSensorRecordDTOList(SensorRecordVO sensorRecordVO) {
        /*根据时间分表查询*/
        Date souceTime = new Date();
        if(sensorRecordVO.getEndTime() !=null){
            souceTime = sensorRecordVO.getEndTime();
        }
        LocalDate localDate =  souceTime.toInstant()          // 将Date转换为Instant
                .atZone(ZoneId.systemDefault())  // 指定时区
                .toLocalDate();        // 转换为LocalDate
        String yearMonthStr = YearMonth.from(localDate).toString();
        yearMonthStr = yearMonthStr.replaceAll("-","");
        sensorRecordVO.setYearMouth(Long.valueOf(yearMonthStr));
        List<SensorRecord> sensorRecordList = sensorRecordMapper.selectSensorRecordListNew(sensorRecordVO);
        List<SensorRecordDTO> sensorRecordDTOList = new ArrayList<>();
        for (SensorRecord senor:sensorRecordList) {
            SensorRecordDTO sensorRecordDTO = new SensorRecordDTO();
            BeanUtils.copyProperties(senor, sensorRecordDTO);
            if(senor.getShipId()!=null){
                ShipRecord shipRecord =  shipRecordMapper.selectShipRecordById(senor.getShipId());
                if(shipRecord !=null){
                    sensorRecordDTO.setShipName(shipRecord.getShipName());
                }
            }
            sensorRecordDTOList.add(sensorRecordDTO);
        }
        return sensorRecordDTOList;
    }

    /**
     * 新增传感器数据记录
     *
     * @param sensorRecord 传感器数据记录
     * @return 结果
     */
    @Override
    public int insertSensorRecord(SensorRecord sensorRecord)
    {
        SensorRecordVO sensorRecordVO = new SensorRecordVO();
        BeanUtils.copyProperties(sensorRecord, sensorRecordVO);
        /*根据时间分库分表存储*/
        LocalDate localDate = sensorRecord.getCreateTime().toInstant()          // 将Date转换为Instant
                .atZone(ZoneId.systemDefault())  // 指定时区
                .toLocalDate();        // 转换为LocalDate
        String yearMonthStr = YearMonth.from(localDate).toString();
        yearMonthStr = yearMonthStr.replaceAll("-","");
        sensorRecordVO.setYearMouth(Long.valueOf(yearMonthStr));
        return sensorRecordMapper.insertSensorRecordByTime(sensorRecordVO);
    }

    public static void main(String[] args) {
        LocalDate localDate = new Date().toInstant()          // 将Date转换为Instant
                .atZone(ZoneId.systemDefault())  // 指定时区
                .toLocalDate();        // 转换为LocalDate
        String yearMonthStr = YearMonth.from(localDate).toString();
        yearMonthStr = yearMonthStr.replaceAll("-","");
        System.out.println(yearMonthStr);
    }
    /**
     * 修改传感器数据记录
     *
     * @param sensorRecord 传感器数据记录
     * @return 结果
     */
    @Override
    public int updateSensorRecord(SensorRecord sensorRecord)
    {
        sensorRecord.setUpdateTime(DateUtils.getNowDate());
        return sensorRecordMapper.updateSensorRecord(sensorRecord);
    }

    /**
     * 批量删除传感器数据记录
     *
     * @param ids 需要删除的传感器数据记录主键
     * @return 结果
     */
    @Override
    public int deleteSensorRecordByIds(String ids)
    {
        return sensorRecordMapper.deleteSensorRecordByIds(Convert.toStrArray(ids));
    }

    @Override
    public int deleteSensorRecordByIdsAndTime(Remove remove) {
        remove.setId(Convert.toStrArray(remove.getIds()));
        /*根据时间分库分表删除*/
        Date souceTime = new Date();
        if(remove.getEndTime() !=null){
            souceTime = remove.getEndTime();
        }
        LocalDate localDate = souceTime.toInstant()          // 将Date转换为Instant
                .atZone(ZoneId.systemDefault())  // 指定时区
                .toLocalDate();        // 转换为LocalDate
        String yearMonthStr = YearMonth.from(localDate).toString();
        yearMonthStr = yearMonthStr.replaceAll("-","");
        remove.setYearMouth(Long.valueOf(yearMonthStr));
        return sensorRecordMapper.deleteSensorRecordByIdsAndTime(remove);
    }

    /**
     * 删除传感器数据记录信息
     *
     * @param id 传感器数据记录主键
     * @return 结果
     */
    @Override
    public int deleteSensorRecordById(Long id)
    {
        return sensorRecordMapper.deleteSensorRecordById(id);
    }
}
